package com.ydd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.RoleDTO;
import com.ydd.dto.RolePageDTO;
import com.ydd.dto.UserPageDTO;
import com.ydd.entity.Rewinfo;
import com.ydd.entity.Role;
import com.ydd.mapper.MenuMapper;
import com.ydd.mapper.RoleMapper;
import com.ydd.mapper.RoleMenuMapper;
import com.ydd.result.Result;
import com.ydd.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.vo.MenuVO;
import com.ydd.vo.PageVO;
import com.ydd.vo.RoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuMapper menumapper;

    public PageVO page(RolePageDTO rolePageDTO) {
        PageHelper.startPage(rolePageDTO.getPage(), rolePageDTO.getLimit());
        Page<RoleVO> page = roleMapper.page(rolePageDTO);
        PageVO pageVO = new PageVO();
        pageVO.setData(page.getResult());
        pageVO.setCount(page.size());
        pageVO.setPageSize(rolePageDTO.getLimit());
        pageVO.setCurrentPage(rolePageDTO.getPage());
        pageVO.setTotalPage(page.getTotal());
        return pageVO;
    }

    @Override
    public void add(RoleDTO role) {
        Role role1 = new Role();
        BeanUtils.copyProperties(role, role1);
        role1.setCreateDate(LocalDateTime.now());
        role1.setUpdateDate(LocalDateTime.now());
        String uuid = UUID.randomUUID().toString();
        role1.setId(uuid);
        //全选时为超级管理员
        role1.setSuperadmin(0);
        role1.setDelFlag("0");
        role.getMenuids().stream().forEach(s -> {
            roleMenuMapper.insertB(uuid,s);
       }
        );
        roleMapper.insert1(role1);
    }

    @Override
    @Transactional
    public void delete(String id) {
        //数组模糊查询
        if (roleMapper.selectBandingUser(id) != 0) {
            throw new RuntimeException("有绑定用户，无法删除");
        }
        //其他表
        roleMenuMapper.deleteById(id);
        //若有user绑定无法删除
        //本地
        roleMapper.delete1(id);

    }

    @Override
    public Rewinfo seleteByIdd(String id) {
        //本表的
        Rewinfo rewinfo = roleMapper.getById(id);
        //获得角色权限对象id
        List<String> list = roleMenuMapper.selectRoleAllmeanu(id);
        //获得角色所有权限对象
        List<MenuVO> menuVOList = getRoleAllMenu(0, list);
        //组转数据
        rewinfo.setAuthList(menuVOList);
        return rewinfo;
    }

    public List<MenuVO> getRoleAllMenu(Integer i, List<String> list) {
        List<MenuVO> sonBypid = menumapper.getSonBypid(i);
        //判断是否为链表里的数值
        List<MenuVO> collect = sonBypid.stream()
                .filter(s -> list.contains(s.getId().toString()))
                .collect(Collectors.toList());
        if (collect.isEmpty()) {
            return null;
        }
        //当里面数字有儿子时 遍历里面的数字 并保存
        for (MenuVO menuVO : collect) {
            menuVO.setChildren(getRoleAllMenu(menuVO.getId().intValue(), list));
        }
        return collect;
    }


    @Override
    public Result allDate() {
        //估计要考虑到分页，先鸽着
        //本表的
        List<Rewinfo> rewinfos = roleMapper.selectall();
        //获得角色各自权限菜单链表
        for (Rewinfo rewinfo : rewinfos) {
            List<String> list = roleMenuMapper.selectRoleAllmeanu(rewinfo.getId());
            //获得角色所有权限对象
            List<MenuVO> menuVOList = getRoleAllMenu(0, list);
            //组转数据
            rewinfo.setAuthList(menuVOList);
        }
        return Result.success(rewinfos);
    }
}
